package uppacket

import (
	"time"

	"gitee.com/yanwc/ebicycle/device/frame"
	"gitee.com/yanwc/ebicycle/device/internal/codec"
	"gitee.com/yanwc/ebicycle/device/internal/field"
	"gitee.com/yanwc/ebicycle/device/server"
	"gitee.com/yanwc/ebicycle/device/utils"

	"github.com/panjf2000/gnet"
)

type HeartBeatPacket struct{}

var _ IPacket = (*HeartBeatPacket)(nil)

func (hbp *HeartBeatPacket) Do(frame frame.IFrame, c gnet.Conn) ([]byte, gnet.Action, error) {
	var csq field.CSQ
	var voltage field.Voltage
	utils.LoadPacket(frame.PayloadData(), csq[:], voltage[:])
	// 更新设备心跳信息
	if d, ok := server.SessionDevices.GetWithConn(c); ok {
		d.LastUpdatedAt = time.Now()
	} else {
		return nil, gnet.Shutdown, nil
	}

	f := &codec.Frame{
		CmdWord: field.CommandWord{0x02},
		SN:      frame.GetSN(),
	}

	return f.WrapPacket(nil), gnet.None, nil
}
